<?php
$title = "Register";
?>
<div id="content">
<?php
	if (!@$_GET['do']) 
	{
		echo "<h1>Registration</h1>";
		if ( $myst['AllowReg'] == 0 )
			{
			echo "<p>Sorry, but our server is not accepting registrations at this time. Please check back later!</p></div></div>";
			}
		else 
		{ 
		?>
		<p>Please take a moment to read the rules below before registering an account. Please fill out all information below to register for an account for <?php echo $myst['ServerName']; ?>.</p>
		<?php if ( $myst['EmailConf'] == 1 )
			{
			echo "<p>After registering, an email will be dispatched to the email address provided. You must read and follow this email for your account to be activated.</p>";
			}
		else 
			{
			echo "<p>After filling out the form, you can login to our server immediately.</p>";
			}
		echo "<center><form action='?module=account&action=register&do=process' method='post'>"; 
		if (count($servers) > 1)
		{
			echo "Please select the server you would like to register for. Some of our servers may be interconnected.";
			?>
				<select name='server'>
				<?php foreach ($servers as $k => $v)
				{
						echo "<option value=$servers[$k]>{$v['ServerName']}";
				}
		}
		?>
				<table>
				<tr><td>Username:</td><td><input type="text" name="user" /></td></tr>
				<tr><td>Password:</td><td><input type="password" name="pass" /></td></tr>
				<tr><td>Password (again):</td><td><input type="password" name="pass2" /></td></tr>
				<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
				<tr><td>Email (again):</td><td><input type="text" name="email2" /></td></tr>
				<tr><td>Sex:</td><td>M <input type="radio" name="sex" value="male" /> &nbsp; F <input type="radio" name="sex" value="female" /></td></tr>
				</table>
				<?php if ($myst['reCaptcha'] == 0 )
				{
					echo "<input type='submit' value='Register!' />";
				}
				else
				{
					?>
					Please type the two words in the image:</td><td><?php echo ReCaptcha(); ?>
					<input type="submit" value="Register!" />
				<?php } ?>
			</form>
		</center>
	<?php }
	}
	elseif (@$_GET['do'] = 'process')
	{
		if(!isset($_POST['user']) || !isset($_POST['pass']) || !isset($_POST['pass2']) || !isset($_POST['email']) || !isset($_POST['email2']) || !isset($_POST['sex'])) {
		echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
		echo "<p align='center'>It appears you have missed a field. Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		elseif (ReCaptcha_Check() == false){
			echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
			echo "<p align='center'>It appears that you did not enter the reCAPTCHA correctly or failed to enter it at all.<br />";
			echo "Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		elseif (strlen($_POST['pass2']) < $myst['MinPassLength'] && strlen($_POST['pass2']) > $myst['MaxPassLength']){
			echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
			echo "<p align='center'>It appears that your password is of the incorrect length! Passwords must be between ".$myst['MinPassLength']." and ".$myst['MaxPassLength']." characters or more.<br />";
			echo "Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		elseif (!eregi("^[^@]*@[^@]*\.[^@]*$", $_POST['email']) || strlen($_POST['email']) > 30){
			echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
			echo "<p align='center'>The email that you entered is in the wrong format! Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		elseif ($_POST['email'] != $_POST['email2']){
			echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
			echo "<p align='center'>The emails that you entered do not match! Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		elseif ($_POST['pass'] != $_POST['pass2']){
			echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
			echo "<p align='center'>The passwords that you entered do not match! Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
		}
		else {
			//All the fields were filled out and in the correct format.
			$userid = mysql_real_escape_string($_POST['user']);//We need to make sure that we're not putting any malicious stuff in the DB's, AND clean up the input TO insert.
			$pass = mysql_real_escape_string($_POST['pass2']);
			$email = mysql_real_escape_string($_POST['email2']);
			//Does the user exist?
			$check_login_exist = mysql_query("SELECT userid FROM login WHERE userid = '$userid'");
			$num_login_exist = mysql_num_rows($check_login_exist); 
			if ($num_login_exist > 0) {//the user exists.
				echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." Incomplete!</h2>";
				echo "<p align='center'>It seems the Account Name you desire is being used by another member! Please <a href='?module=account&action=register'>go back</a>, and try again.</p>";
			}
			else { // the user does not exist.
				$time = date('Y-m-d H:i:s'); // Get the current time.
				//TODO: Function to retrieve the current IP of the user.
				$key = GetMD5Key(); // Get the random key for use when confirming.
				// TODO: Make a function to make sure that the key is unique in the database. It should always be unique...but just in case...
				if ($myst['UseMD5'] = 1)
				{
				$pass = md5($pass); // Get the MD5 hash for the password.
				}
				//OK, we've clean the input and checked to see if the user existed. We can go ahead and insert it into the pending table.
				mysql_query("INSERT INTO `cp_pending` (userid,pass,email,sex,time,key,ip) VALUES('$userid','$pass','$sex','$email','$time','$key','$ip')");	
				echo "<h2 class='title' align='center'>Registration for ".$myst['ServerName']." complete!</h2>";
				echo "<p align='center'>Check the email address provided for more instructions!</p>";
			}
		}
	} ?>
    </div>
  </div>
<!-- end main -->
